package ui;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import each_class.SongClass;
import java.util.Collection;
import java.util.List;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.graphics.Font;

public class MusicView extends Composite {

	protected ScrolledView scrolledView;
	
	
	public MusicView(Composite parent, int style) {
		super(parent, style);
		setSize(1210, 513);
		
		Label musicName = new Label(this, SWT.NONE);
		musicName.setBounds(45, 10, 350, 17);
		musicName.setText("歌曲");
		
		Label singer = new Label(this, SWT.NONE);
		singer.setText("歌手");
		singer.setBounds(401, 10, 343, 17);
		
		Label album = new Label(this, SWT.NONE);
		album.setText("专辑");
		album.setBounds(750, 10, 298, 17);
		
		Label time = new Label(this, SWT.NONE);
		time.setText("时长");
		time.setBounds(1054, 10, 156, 17);
		
		
		scrolledView  = new ScrolledView(this, SWT.NONE) {
			@Override
			public void reachBottom() {
				if (!isWarned) {

					isWarned =! isWarned;
					downcount += scrollHeight;
					Label warnning = new Label(scrolledView, SWT.CENTER);
					warnning.setFont(new Font(getDisplay(), "Microsoft YaHei UI", 11, SWT.NORMAL));
					warnning.setSize(width, scrollHeight);
					warnning.addMouseListener(new MouseAdapter() {
						public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
							warnning.setText("加载中。。。。。");
							new Thread() {
								public void run() {
									List<SongClass> musicList = ui.global.Operation.searchMore();
									getDisplay().asyncExec(new Runnable() {
										
										@Override
										public void run() {
											warnning.dispose();
											downcount -= scrollHeight;
											scrollDown();
											((MusicView)getParent()).loadMusic(musicList);
											isWarned =! isWarned;
										}
									});
								};
								 
							}.start();
						};
					});
					warnning.setText("我是有底线的的！");
					scrolledView.layout();
				}
			}
		};
		scrolledView.setBounds(0, 33, 1210, 480);
		scrolledView.setScrollHeight(32);
	}
	
	public void loadMusic(Collection<SongClass> musicList) {
		for (SongClass music : musicList) {
			if (music != null) {
				new MusicViewItem(scrolledView, SWT.NONE).initail(music);
				scrolledView.downcount += scrolledView.scrollHeight;
			}
		}
		scrolledView.layout();
	}
	

}
